public class Solution3 {
    //740. 删除并获得点数
    public int deleteAndEarn(int[] nums) {

        int maxN = nums[0];
        for (int num : nums) {
            maxN = Math.max(maxN,num);
        }
        int[] arr = new int[maxN+1];
        for (int i = 0; i < nums.length; i++) {
            arr[nums[i]] += nums[i];
        }
        int[] f = new int[maxN+1]; // 选i....
        int[] g = new int[maxN+1];  // 不选i....
        f[0] = arr[0];
        for (int i = 1; i <= maxN; i++) {
            f[i] = g[i-1] + arr[i];
            g[i] = Math.max(g[i-1],f[i-1]);
        }
        return Math.max(f[maxN],g[maxN]);
    }
}
